package com.mcptt.config.body.info.mcdata;


import android.webkit.MimeTypeMap;

import com.cloudptt.api.comm.Common;
import com.cloudptt.api.comm.FileOperation;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.log.Log;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class FileMataInfo {
    public final static String Key_Header ="file-selector:";
    public final static String Key_Name ="name";
    public final static String Key_Type ="type";
    public final static String Key_Size ="size";
    private Map<String,String> valueMap = null;

    private FileMataInfo()
    {
        valueMap = new HashMap<String,String>();
    }


    public static FileMataInfo parse(String infoStr)
    {
        Log.debug(FileMataInfo.class, "parse infoStr = " + infoStr);
        FileMataInfo info = new FileMataInfo();
        if(infoStr == null
                || infoStr.length() <= 0)
        {
            return info;
        }
        if(infoStr.contains(Key_Header)) {
            Log.debug(FileMataInfo.class, "parse infoStr has Key_Header");
            infoStr = infoStr.replace(Key_Header,"");
//            int index = infoStr.indexOf(Key_Header);
//            Log.debug(FileMataInfo.class, "parse index = " + index);
//            if (index > 0) {
//                infoStr = infoStr.substring(index);
//            }
        }
        Log.debug(FileMataInfo.class, "parse infoStr = " + infoStr);
        String[] strArr = infoStr.split(" ");
        for(int i = 0; i < strArr.length; i++)
        {
            String value = strArr[i];
            Log.debug(FileMataInfo.class, "parse value = " + value);
            if(value.contains(":"))
            {
                String[] valueArr = value.split(":");
                if(valueArr.length == 2
                        && valueArr[0] != null
                        && valueArr[1] != null)
                {
                    String key = valueArr[0].trim();
                    String val = valueArr[1].trim();
                    Log.debug(FileMataInfo.class, "parse key = " + key + " val = " + val);
                    info.valueMap.put(key, val);
                }
            }
        }
        return info;
    }

    public String getFileName()
    {
        String value = getValue(Key_Name);
        if(value != null)
        {
            value = value.replace("\"","");
            value = value.trim();
//            value = FileOperation.abbreviateFileName(value,20);
        }
        return value;
    }
    public String getSizeStr()
    {
        String value = getValue(Key_Size);
        Log.debug(FileMataInfo.class, "getSizeStr value = " + value);
        if(value != null)
        {
            value = value.replace("\"","");
            value = value.trim();
            value = FileOperation.getReadableFileSize(value);
        }
        Log.debug(FileMataInfo.class, "getSizeStr end value = " + value);
        return value;
    }

    public String getVoiceSizeStr()
    {
        String value = getValue(Key_Size);
        return value;
    }

    public String getType()
    {
        String value = getValue(Key_Type);
        if(value != null)
        {
            value = value.replace("\"","");
            value = value.trim();
        }
        return value;
    }

    private String getValue(String key)
    {
        if(key == null
                || key.equals(""))
        {
            return "";
        }

        String value = valueMap.get(key);
        if( value != null ) {
//            value = value.replace("\"","");
//            value = value.trim();
            return value;
        }

        return "";
    }

    public static String toMetaData(File file,long sec)
    {
        String fileName = file.getName();
        String fileExtension = FileOperation.getFileExtension(fileName);
        String fileType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
        long size = file.length();
        StringBuffer strBuf = new StringBuffer(Key_Header);
        strBuf.append(Key_Name).append(":\"").append(fileName).append("\" ");
        strBuf.append(Key_Type).append(":").append(fileType).append(" ");

        if(sec != 0)
        {
            size = sec;
        }
        strBuf.append(Key_Size).append(":").append(size);
        return  strBuf.toString();
    }
}
